import { defineStore } from 'pinia';
import { Datasource } from 'src/types';
import { initDatabaseConnection } from 'src/api/DatabaseApi';
import { ref, toRaw, watch } from 'vue';
import { Pool } from 'mysql';

export const useDatabaseStore = defineStore('database', () => {
  //变量
  //连接池
  const pool: ref<Pool | null> = ref(null);
  const datasource = ref({} as Datasource);
  //获取
  const stmtDatabase = (sql: string) => {
    return new Promise(async (resolve) => {
      resolve(await stmtDatabase(sql));
    });
  };
  watch(datasource, async () =>
  {
    await initDatabaseConnection(toRaw(datasource.value));
  });
  return {
    pool,
    datasource,
    stmtDatabase,
  };
});
